package com.yjy.fuyao.stu.controller.admin;


import com.yjy.fuyao.stu.entity.Chapter;
import com.yjy.fuyao.stu.entity.vo.ChapterVo;
import com.yjy.fuyao.stu.service.ChapterService;
import com.yjy.fuyao.utils.exception.BusinessException;
import com.yjy.fuyao.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yjy
 * @since 2021-08-17
 */

@Api(tags="课程章节管理")
@RestController
@RequestMapping("/admin/stu/chapter")
public class AdminChapterController {

    @Resource
    private ChapterService chapterService;

    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("/nestedList/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }

    @ApiOperation(value = "保存或更新章节")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@ApiParam(name = "chapter",value = "章节对象", required = true)
                          @RequestBody Chapter chapter){
        System.out.println("chapter================="+chapter);
        try {
            chapterService.saveOrUpdate(chapter);
        } catch (Exception e) {
            throw new BusinessException("保存失败",20001,e);
        }
        return R.ok();
    }

    @ApiOperation(value = "删除章节")
    @DeleteMapping("/remove")
    public R removeById(@ApiParam(name = "id",value = "章节Id", required = true)
                        @RequestParam("id") String id){
        try {
            chapterService.removeChapterById(id);
        } catch (BusinessException e) {
            throw e;
        }
        return R.ok();
    }

}

